package pl.project.engineering.pages;

import java.io.IOException;
import java.io.InputStream;
import java.util.Date;
import java.util.List;
import org.apache.shiro.authz.annotation.RequiresAuthentication;
import org.apache.shiro.authz.annotation.RequiresGuest;
import org.apache.tapestry5.StreamResponse;
import org.apache.tapestry5.annotations.*;
import org.apache.tapestry5.ioc.annotations.*;
import org.apache.tapestry5.corelib.components.*;
import org.apache.tapestry5.internal.services.LinkSource;
import org.apache.tapestry5.services.Response;
import pl.project.engineering.data.SensorsDto;
import pl.project.engineering.services.AppConfig;
import pl.project.engineering.services.LogFileList;
import pl.project.engineering.services.Repository;
import pl.project.engineering.services.SerialComPort;

/**
 * Start page of application Project-engineering.
 */
@Import(stylesheet = {"context:css/nv.d3.css"},
library = {"context:js/d3.v2.js",
"context:js/nv.d3.js",
"context:js/utils.js",
"context:js/tooltip.js",
"context:js/legend.js",
"context:js/axis.js",
"context:js/scatter.js",
"context:js/line.js",
"context:js/multiBar.js",
"context:js/multiChart.js"})

public class Index {

  @Inject
  private Repository repo;
  @Inject
  private AppConfig config;
  @Inject
  private LogFileList logFileList;
  @Inject
  private SerialComPort serialComPort;
  
  private SensorsDto dto;
  @Property
  private float temp;
  @Property
  private List<String> logFiles;
  @Property
  private String fileName;
  @InjectComponent
  private Zone zone;

  @InjectPage
  private FirstRunConfiguration firstRunConfiguration;
  @Inject
  private LinkSource linkSource;
  

  void setupRender() {
    dto = repo.getLasMeasurment();
    logFiles = logFileList.getLogFiles();
  }

  public String getTemperature() {
    return temp != -200 ? String.format("%.2f\u00B0C;", temp) : null;
  }

  public Date getDtoTime() {
    return new Date(repo.getLasMeasurment().getTime());
  }
  
  public String getPageUrl() {
    return linkSource.createPageRenderLink("JsonChartData", false).toAbsoluteURI().toString();
  }
  
  public String getDeviceState(){
    if(serialComPort.isConnected()){
      return "Device connected \nto com port: " + serialComPort.getPortName();
    } else {
      return "Device disconnected";
    }
  }

  Object onActionFromDownload(final String fileName) {

    return new StreamResponse() {
      @Override
      public String getContentType() {
        return "text/plain";
      }

      @Override
      public InputStream getStream() throws IOException {
        return logFileList.getFileAsStream(fileName);
      }

      @Override
      public void prepareResponse(Response rspns) {
        rspns.setHeader("Content-disposition",
                "Attachment; filename=" + fileName);
      }
    };
  }


  Object onActivate() {
    if (!config.isLoaded()) {
      return firstRunConfiguration;
    } else {
      return null;
    }
  }
  
  public SensorsDto getDto() {
    return dto = repo.getLasMeasurment();
  }
}
